package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import domain.Distrito;
import domain.Tema;

public class JDBCTemaDAO implements ITemaDAO {

private Connection conn;
	
	public JDBCTemaDAO(){
		conn = ConnectionManager.getInstance().checkOut();
		System.out.println("SE HA CREADO LA CONEXION");
	}
	@Override
	public List<Tema> selectAllTemas() {
		// TODO Auto-generated method stub
		PreparedStatement stmt=null;
		ResultSet result=null;
		List<Tema> searchResults=new LinkedList<Tema>();
		
		String sql="SELECT * FROM temas";
		
		try{
			stmt=conn.prepareStatement(sql);
			stmt.executeQuery();
			result=stmt.executeQuery();
			
			while(result.next()){
				
				Tema t = new Tema();
				t.setIdTema(result.getString("OIDTema"));
				t.setNombre(result.getString("nombre"));
				searchResults.add(t);
			}
				
			} catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
		return searchResults;
	}

}
